package yunjiao.javatutorials.guava.concurrent;

import com.google.common.util.concurrent.RateLimiter;

/**
 * 批量获取令牌
 *
 * @author yangyunjiao
 */
public class BatchAcquireExample {
    public static void main(String[] args) {
        RateLimiter rateLimiter = RateLimiter.create(10.0); // 每秒10个许可

        // 批量获取5个令牌
        double waitTime = rateLimiter.acquire(5);
        System.out.printf("批量获取5个令牌，等待时间: %.2f秒%n", waitTime);

        // 后续请求
        for (int i = 0; i < 5; i++) {
            waitTime = rateLimiter.acquire();
            System.out.printf("任务 %d 执行，等待时间: %.2f秒%n", i, waitTime);
        }
    }
}
